package org.ddr.board.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ddr.board.dao.BbsDAO;
import org.ddr.board.vo.BbsVO;
import org.ddr.board.vo.BoardVO;
import org.ddr.servlet.URIServlet;

/**
 * Servlet implementation class BbsController
 */
@WebServlet("/bbs/*")
public class BbsController extends URIServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public BbsController() {
        super();
    }

	/**
	 * @throws Exception 
	 * @throws NumberFormatException 
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
    public void list(HttpServletRequest request, HttpServletResponse response) throws NumberFormatException, Exception {
    	log.info("--------------list--------------");
		
    	String pageno = request.getParameter("pageno");
    	
    	if(pageno == null) {
    		pageno =  "1";
    		
    		Double totalCnt = 0D;
    		List<BoardVO> list = new ArrayList<BoardVO>();
    		list = BbsDAO.instance.read(Integer.parseInt(pageno));
    		
    		for(BoardVO vo : list) {
    			totalCnt = vo.getTotalCnt();
    		}
    		request.setAttribute("list", list);
    		request.setAttribute("pageno", pageno);
    		request.setAttribute("totalCnt", totalCnt);
    		
    	}
    	
		request.getRequestDispatcher(VIEW_PREFIX+request.getRequestURI()+VIEW_APPENDIX).forward(request, response);
		
	}
    
    public void boardcontent(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	log.info("---------------boardcontent--------------");
    	
    	request.getRequestDispatcher(VIEW_PREFIX+request.getRequestURI()+VIEW_APPENDIX).forward(request, response);
    	
    }
}